<?php
namespace app\admin\controller;

use think\Controller;
use app\common\model\admin\AdminMenu;
use think\Db;
use app\common\model\cash\CashList;
use app\common\model\coin\CoinUser;

class Index extends Base
{
	public function index()
	{
		//获取菜单数据
		$admin_id = 1;
		if($admin_id == 1) {  //超级管理员获取全部权限
			$menu = AdminMenu::where(['parent_id' => 0 ,'state' => 1])->select()->toArray();
			foreach ($menu as &$rows) {
				$rows['child'] = AdminMenu::where(['parent_id' => $rows['id'] ,'state' => 1])->select()->toArray();
			}
		}
		return jsonSuccess($menu);
	}
	public function getIndexInfo()
	{
		//获取今日注册
		$start_time = strtotime(date('Y-m-d').'00:00:00');
		$end_time = strtotime(date('Y-m-d').'23:59:59');
		$recharge = Db::name('cash_recharge')->where(['coin_id' => 5,'state' => 1,'create_time' => ['between',[$start_time,$end_time]]])->sum('num');
		//获取提现数据
		$cash = Db::name('cash_list')->where(['coin_id' => 5,'state' => 1,'create_time' => ['between',[$start_time,$end_time]]]) -> sum('num');
		//获取会员量
		$user_num = Db::name('users')->count();
		
		$day_num = Db::name('users')->where(['create_time' => ['between',[$start_time,$end_time]]])->count();
		$day_order = Db::name('trade_leveraged')->where(['create_time' => ['between',[$start_time,$end_time]]])->count();
		//总手续费
		$fee = Db::name('trade_leveraged')->where(['create_time' => ['between',[$start_time,$end_time]],'state' => ['neq',2]])->sum('fee');
		//总盈利
		$loss_up_buy = Db::name('trade_leveraged')->where(['trade_style' => 1,'close' => ['exp',Db::raw('> buy_price')],'create_time' => ['between',[$start_time,$end_time]],'state' => ['neq',2]])->sum('real_price');
		$loss_up_sale = Db::name('trade_leveraged')->where(['trade_style' => 2,'close' => ['exp',Db::raw('< buy_price')],'create_time' => ['between',[$start_time,$end_time]],'state' => ['neq',2]])->sum('real_price');
		$loss_up = $loss_up_buy + $loss_up_sale;
		//总亏损
		$loss_down_buy = Db::name('trade_leveraged')->where(['trade_style' => 1,'close' => ['exp',Db::raw('< buy_price')],'create_time' => ['between',[$start_time,$end_time]],'state' => ['neq',2]])->sum('real_price');
		$loss_down_sale = Db::name('trade_leveraged')->where(['trade_style' => 2,'close' => ['exp',Db::raw('> buy_price')],'create_time' => ['between',[$start_time,$end_time]],'state' => ['neq',2]])->sum('real_price');
		$loss_down = $loss_down_buy + $loss_down_sale;
		//获取消息记录
		$message = [];
		$cash_record = Db::name('cash_list')->where(['state' => 0])->order('id desc')->limit(5)->select();
		foreach ($cash_record as $row) {
			$user_name = Db::name('users')->where(['id' => $row['user_id']])->value('user_name');
			$item = "用户{$user_name}发起了一条提币申请";
			$message[] = $item;
		}
		$recharge_recorde = Db::name('cash_recharge')->where(['state' => 0])->order('id desc')->limit(5)->select();
		foreach ($recharge_recorde as $row) {
			$user_name = Db::name('users')->where(['id' => $row['user_id']])->value('user_name');
			$item = "用户{$user_name}发起了一条提币申请";
			$message[] = $item;
		}
		//所有用户余额
		$user_account = CoinUser::where(['coin_id' => 5,'state' => 1])->sum('avalible_price');
		
		$res = [ 
				'cash' => $cash,
				'recharge' => $recharge,
				'user_num' => $user_num,
				'day_num' => $day_num,
				'day_order' => $day_order,
				'fee' => $fee,
				'loss_up' => $loss_up,
				'loss_down' => $loss_down,
				'message' => $message,
				'user_account' => $user_account
		];
		return jsonSuccess($res);
	}
}